<template>
  <div>
    <ContentHeader :title="store.student.name + '的学习课程'" v-if="store.student"></ContentHeader>
    <ContentBody v-loading="store.loading">
      <el-table :data="store.items"
        :stripe="true"
        :highlight-current-row="true"
      >
        <el-table-column property="id" width="80" />
        <el-table-column property="lessonTitle" label="课程" />
        <el-table-column property="studyTimes" label="学习时长" />
        <el-table-column property="createdAt" label="首次开通时间" :formatter="(row: any) => timeFormatter(row.createdAt)" />
        <el-table-column property="expireTime" label="过期时间" />
        <el-table-column label="操作">
          <template #default="scope">
            <el-button plain @click="handleLog(scope.row)">详细记录</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #pagination>
      <el-pagination background 
        hideOnSinglePage
        layout="prev, pager, next"
        :total="store.pager.total"
        :page-size="store.pager.pageSize"
        @current-change="handlePageChange"
      />
      </template>
    </ContentBody>
  </div>
</template>

<script setup lang="ts">
import { onMounted, onUpdated } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { useStudentStudyStore } from '@/stores/studentStudy'
import { ElMessage } from 'element-plus';
import { timeFormatter } from '@/utils/utils';

const store = useStudentStudyStore()
const route = useRoute()
const router = useRouter()

const handlePageChange = (page: number) => {
  store.changePage(page, store.search.limit)
}

const handleLog = (row: any) => {
  router.push({name: 'education_students_study_log', params: {accountId: row.accountId, lessonId: row.lessonId}})
}

onMounted(() => {
  if (route.params.id) {
    store.loadItems(parseInt(route.params.id as string))
  } else {
    ElMessage.error('非法参数')
    router.back()
  }
})

onUpdated(() => {
  if (store.student && route.params.id && store.student.id !== parseInt(route.params.id as string)) {
    store.loadItems(parseInt(route.params.id as string))
  }
})
</script>